import 'package:flutter/material.dart';
// import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyText extends StatelessWidget {
  final dynamic text; // 文本
  final dynamic color; // 颜色
  final bool bold; // 加粗
  final double? fontSize; // 大小
  final double? height; // 行高(比例值)
  final int? maxLines; // 最大行数
  final TextAlign? textAlign; // 对齐方式
  final TextOverflow? overflow; // 截断方式
  final List<MySpan>? children; // 内联子组件
  const MyText(
    this.text, {
    super.key,
    this.bold = false,
    this.color = const Color(0xFF1A1A1A),
    this.maxLines,
    this.fontSize,
    this.textAlign,
    this.height = 1.4, // 等于高 44.8.w
    this.overflow,
    this.children,
  });

  // 内联列表
  List<InlineSpan> getSpanList(InlineSpan value, List<MySpan> children) {
    return [
      value,
      ...children.map(
        (MySpan item) => TextSpan(
          text: (item.text ?? '').toString(),
          style: TextStyle(
            fontFamily: "Din",
            height: item.height,
            color: item.color is Color ? item.color : Color(int.parse('0xFF${item.color}')),
            // fontSize: item.fontSize ?? 32.w,
            decoration: TextDecoration.none,
            fontWeight: item.bold! ? FontWeight.bold : null,
          ),
        ),
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    TextStyle style = TextStyle(
      fontFamily: "Din",
      height: height,
      color: color is Color ? color : Color(int.parse('0xFF$color')),
      // fontSize: fontSize ?? 32.w,
      decoration: TextDecoration.none,
      fontWeight: bold ? FontWeight.bold : FontWeight.normal,
    );
    return children == null
        ? Text(
            (text ?? '').toString(),
            textAlign: textAlign,
            maxLines: maxLines,
            overflow: overflow,
            style: style,
          )
        : Text.rich(
            TextSpan(
              children: getSpanList(
                TextSpan(text: text.toString(), style: style),
                children!,
              ),
            ),
            textAlign: textAlign,
            maxLines: maxLines,
            overflow: overflow,
            style: style,
          );
  }
}

// 内联文本
class MySpan {
  final dynamic text; // 文本
  final dynamic color; // 颜色
  final bool? bold; // 加粗
  final double? fontSize; // 大小
  final double? height; // 行高(比例值)
  const MySpan(
    this.text, {
    this.bold = false,
    this.color = const Color(0xFF1A1A1A),
    this.fontSize,
    this.height = 1.4,
  });
}
